package com.huawei.image.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.image.v2.ImageClient;
import com.huaweicloud.sdk.image.v2.model.ImageMainObjectDetectionReq;
import com.huaweicloud.sdk.image.v2.model.RunImageMainObjectDetectionRequest;
import com.huaweicloud.sdk.image.v2.model.RunImageMainObjectDetectionResponse;
import com.huaweicloud.sdk.image.v2.region.ImageRegion;

public class ImageMainObjectDetectionDemo {
    
    public static void main(String[] args) {
        // 设置AK和SK
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
    
        ICredential auth = new BasicCredentials()
            .withAk(ak)
            .withSk(sk);
    
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        ImageClient client = ImageClient.newBuilder()
            .withCredential(auth)
            .withRegion(ImageRegion.valueOf("cn-north-4")) // 示例中区域为北京四，根据实际调用区域替换
            .withHttpConfig(config)
            .build();
        RunImageMainObjectDetectionRequest request = new RunImageMainObjectDetectionRequest();
        ImageMainObjectDetectionReq body = new ImageMainObjectDetectionReq();
        // 替换示例中的图像地址
        body.setUrl("图片url地址");
        request.withBody(body);
        try {
            RunImageMainObjectDetectionResponse response = client.runImageMainObjectDetection(request);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}